$(function() {
    $("#loginBtn").on("tap", function() {
        var data = {
            username: $("input[name='username']").val(),
            password: $("input[name='password']").val(),
        }
        if (!data.username) {
            mui.toast('请输入用户名');
            return;
        }
        if (!data.password) {
            mui.toast('请输入密码');
            return;
        }
        $.ajax({
            type: "post",
            url: ajaxUrl('login'),
            data: data,
            beforeSend: function() {
                $("#loginBtn").html("正在登录....")
            },
            success: function(result) {
                if (result.success) {
                    mui.toast("登录成功");
                    setTimeout(function() {
                        var url = localStorage.getItem('returnUrl');
                        if (url) {
                            location.href = url;
                        } else {
                            location.href = "user.html";
                        }
                    }, 2000);
                } else {
                    mui.toast(result.message);
                }
                $("#loginBtn").html("登录");
            }
        })
    })
})